package com.fw.payment.bao;

import static org.springframework.beans.factory.config.BeanDefinition.SCOPE_PROTOTYPE;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.fw.payment.service.ServiceExecutor;
import com.fw.payment.service.ServiceOrder;
import com.fw.service.bean.EntryBean;
import com.fw.service.configuration.ConfigurationService;
import com.fw.service.globalsetting.bean.GlobalSettingBean;
import com.fw.service.passwordpolicy.bean.PasswordPolicyBean;

@Component
@Scope(SCOPE_PROTOTYPE)
public class ConfigurationBAO extends AbstractBAO implements Serializable {

    private static final long serialVersionUID = 1L;

    @Autowired
    private ConfigurationService configurationService;

    @SuppressWarnings("unchecked")
    public Map<String, Serializable> updatePasswordPolicy(PasswordPolicyBean passwordPolicyBean) throws Exception {
	try {
	    ServiceOrder serviceOrder = new ServiceOrder();
	    serviceOrder.setServiceClass(this.configurationService);
	    serviceOrder.setMethodName("updatePasswordPolicy");
	    serviceOrder.setParameterTypes(new Class[] { PasswordPolicyBean.class });
	    serviceOrder.setParameterValues(new Object[] { passwordPolicyBean });

	    ServiceExecutor serviceExecutor = new ServiceExecutor(serviceOrder, timeout);
	    serviceOrder = serviceExecutor.callService();
	    if (serviceOrder.getResultCode() != null && serviceOrder.getResultCode().equals(new Integer(0))) {
		List<EntryBean<String, Serializable>> entryBeans = (List<EntryBean<String, Serializable>>) serviceOrder.getReturnValue();
		if (entryBeans != null) {
		    Map<String, Serializable> map = new HashMap<String, Serializable>();
		    for (EntryBean<String, Serializable> entryBean : entryBeans) {
			map.put(entryBean.getKey(), entryBean.getValue());
		    }
		    return map;
		}
	    }
	} catch (Exception e) {
	    throw e;
	}
	return null;
    }

    @SuppressWarnings("unchecked")
    public Map<String, Serializable> updateGlobalSetting(GlobalSettingBean globalSettingBean) throws Exception {
	try {
	    ServiceOrder serviceOrder = new ServiceOrder();
	    serviceOrder.setServiceClass(this.configurationService);
	    serviceOrder.setMethodName("updateGlobalSetting");
	    serviceOrder.setParameterTypes(new Class[] { GlobalSettingBean.class });
	    serviceOrder.setParameterValues(new Object[] { globalSettingBean });

	    ServiceExecutor serviceExecutor = new ServiceExecutor(serviceOrder, timeout);
	    serviceOrder = serviceExecutor.callService();
	    if (serviceOrder.getResultCode() != null && serviceOrder.getResultCode().equals(new Integer(0))) {
		List<EntryBean<String, Serializable>> entryBeans = (List<EntryBean<String, Serializable>>) serviceOrder.getReturnValue();
		if (entryBeans != null) {
		    Map<String, Serializable> map = new HashMap<String, Serializable>();
		    for (EntryBean<String, Serializable> entryBean : entryBeans) {
			map.put(entryBean.getKey(), entryBean.getValue());
		    }
		    return map;
		}
	    }
	} catch (Exception e) {
	    throw e;
	}
	return null;
    }
}
